package com.akuhome.resource.service;

import com.akuhome.resource.DO.message.MessageConfig;
import com.akuhome.resource.DO.message.MessageRecord;
import com.akuhome.resource.DO.message.MessageTemplate;
import com.akuhome.resource.DO.message.MessageType;
import com.akuhome.resource.dao.CharacterDao;
import com.akuhome.resource.dao.MessageDao;
import com.akuhome.resource.entity.Character;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageService {
    @Autowired
    MessageDao messageDao;

    @Autowired
    CharacterDao characterDao;

    public List<MessageTemplate> findAllMessageTemplate(){
        return messageDao.findAllMessageTemplate();
    }

    public MessageTemplate findMessageTemplateById(Integer messageTemplateId){
        MessageTemplate messageTemplate = messageDao.findMessageTemplateById(messageTemplateId);
        if(messageTemplate != null){
            return messageTemplate;
        }else{
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public Integer insertMessageTemplate(MessageTemplate messageTemplate){
        return messageDao.insertMessageTemplate(messageTemplate);
    }

    public Object updateMessageTemplate(MessageTemplate messageTemplate){
        Integer row = messageDao.updateMessageTemplate(messageTemplate);
        if(row == 1){
            return null;
        }else if(row == 0) {
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }else{
            throw new ResourceException(ResultEnum.UNKNOWN_ERROR);
        }
    }

    public Object deleteMessageTemplateById(Integer messageTemplateId){
        Integer row = messageDao.deleteMessageTemplateById(messageTemplateId);
        if(row == 1){
            return null;
        }else if(row == 0) {
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }else{
            throw new ResourceException(ResultEnum.UNKNOWN_ERROR);
        }
    }

    public List<MessageConfig> findAllMessageConfig(){
        return messageDao.findAllMessageConfig();
    }

    public MessageConfig findMessageConfigById(Integer messageConfigId){
        MessageConfig res = messageDao.findMessageConfigById(messageConfigId);
        if(res != null) {
            return res;
        }else{
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public void updateMessageConfigEnableById(Integer messageConfigId,Boolean enable){
        Integer row = messageDao.updateMessageConfigEnableById(messageConfigId,enable);
        if(row == 0){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }else if(row != 1){
            throw new ResourceException(ResultEnum.UNKNOWN_ERROR);
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class,Exception.class, SQLException.class})
    public void updateMessageConfig(MessageConfig messageConfig){
        //先进行本表的更新
        Integer updateRow = messageDao.updateMessageConfig(messageConfig);
        if(updateRow == 0){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }else if(updateRow != 1){
            throw new ResourceException(ResultEnum.UNKNOWN_ERROR);
        }
        //旧的角色列表:characters,新的角色列表:messageConfig.getTarget()
        //获取旧的角色列表
        List<Character> characters = characterDao.findCharactersByMessageConfigId(messageConfig.getId());
        //复制一份作为需要删除的列表
        List<Character> deleteList = new ArrayList<>(characters);
        //TODO 可以换为复制模式
        List<Character> insertList = messageConfig.getTarget();
        //将将删除列表与新的列表作差积,得出需要删除的元素
        deleteList.removeAll(insertList);
        //将新的列表与老的列表作差积,得出需要新增的元素
        insertList.removeAll(characters);
        if(deleteList.size()>0){
            //准备参数
            Map<String,Object> deleteMap = new HashMap<>();
            deleteMap.put("configId",messageConfig.getId());
            deleteMap.put("deleteList",deleteList);
            //删除旧元素
            messageDao.deleteMessageConfigTargetByConfigId(deleteMap);
        }
        if(insertList.size()>0){
            //准备参数
            Map<String,Object> insertMap = new HashMap<>();
            insertMap.put("configId",messageConfig.getId());
            insertMap.put("insertList",insertList);
            //增添新元素
            messageDao.insertMessageConfigTargets(insertMap);
        }
    }

    public List<MessageRecord> findAllMessageRecord(){
        return messageDao.findAllMessageRecord();
    }

    public MessageRecord findMessageRecordById(Integer id){
        return messageDao.findMessageRecordById(id);
    }

    public List<MessageType> findAllMessageType(){
        return messageDao.findAllMessageType();
    }
}
